//
//  ZSTopDownHUD.m
//  CheFu365
//
//  Created by safiri on 2017/9/25.
//  Copyright © 2017年 safiri. All rights reserved.
//

#import "ZSTopDownHUD.h"
#import "ZSTopDownView.h"

@interface ZSTopDownHUD ()

/// The HUD View
@property (nonatomic ,strong) ZSTopDownView *topDownView;

/// 时间累计器
@property (nonatomic ,assign) NSTimeInterval hideTimes;

@property (nonatomic ,strong) NSTimer *dismissTimer;

@end
@implementation ZSTopDownHUD

static ZSTopDownHUD *_shareHud = nil;
+ (ZSTopDownHUD *)shareHUD {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _shareHud = [[ZSTopDownHUD alloc] init];
    });
    return _shareHud;
}
- (instancetype)init {
    self = [super init];
    if (self) {
        _topDownView = [[ZSTopDownView alloc] init];
    }
    return self;
}

#pragma mark - show or hide HUD
+ (void)showHUDAutoHideWithType:(HUDViewType)type hint:(NSString *)hintText {
    [[ZSTopDownHUD shareHUD] showHUDWithType:type hint:hintText];
    [[ZSTopDownHUD shareHUD] hideHUDWithTimeIntervalToHide:0.7 completion:nil];
}
+ (void)showHUDAutoHideWithType:(HUDViewType)type
                          image:(nullable UIImage *)hintImage
                           hint:(NSString *)hintText
                  hintTextColor:(nullable UIColor *)textColor
                backgroundColor:(nullable UIColor *)backgroundColor {
    [[ZSTopDownHUD shareHUD] showHUDWithType:type image:hintImage hint:hintText hintTextColor:textColor backgroundColor:backgroundColor];
    [[ZSTopDownHUD shareHUD] hideHUDWithTimeIntervalToHide:0.7 completion:nil];
}
- (void)showHUDWithType:(HUDViewType)type hint:(NSString *)hintText {
    [self.topDownView configHUDViewType:type message:hintText];
    [self showHUD];
}

- (void)showHUDWithType:(HUDViewType)type image:(nullable UIImage *)hintImage hint:(NSString *)hintText hintTextColor:(nullable UIColor *)textColor backgroundColor:(nullable UIColor *)backgroundColor {
    [self.topDownView configHUDViewType:type image:hintImage message:hintText messageTextColor:textColor backgroundColor:backgroundColor];
    [self showHUD];
}

- (void)showHUD {
    ///每次弹窗出现前，先判断定时器对象是否已经销毁置空，如果没有，先销毁置空
    [self releaseTimer];//销毁置空定时器
    dispatch_async(dispatch_get_main_queue(), ^{
        [[UIApplication sharedApplication].keyWindow addSubview:self.topDownView];
        [self.topDownView show];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapTopDownView)];
        tap.cancelsTouchesInView = NO;
        [self.topDownView addGestureRecognizer:tap];
    });
}
///立即移除弹窗，同时销毁定时器
- (void)hideHUDImmediatelyCompletion:(HideHudCompletion)completion {
    self.hideHudCompletionBlock = completion;
    [self releaseTimer];
    [self.topDownView hide:nil];
    if (self.hideHudCompletionBlock) {
        self.hideHudCompletionBlock();
    }
}
///延时移除弹窗，之后销毁定时器
- (void)hideHUDWithTimeIntervalToHide:(NSTimeInterval)timeInterval completion:(HideHudCompletion)completion {
    self.timeIntervalToHide = timeInterval;
    self.hideHudCompletionBlock = completion;
    if (self.dismissTimer == nil) {
        self.dismissTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(dismissAlertWithTimer) userInfo:nil repeats:YES];
    }
}

#pragma mark - 定时器/手势/block
- (void)dismissAlertWithTimer {
    if (self.hideTimes > self.timeIntervalToHide) {
        [self.topDownView hide:nil];
        if (self.hideHudCompletionBlock) {
            self.hideHudCompletionBlock();
        }
        [self releaseTimer];
    }
    self.hideTimes += 1;
}

///销毁定时器对象，同时将定时器时间归零
- (void)releaseTimer {
    self.hideTimes = 0;
    [self.dismissTimer invalidate];
    self.dismissTimer = nil;
}

///弹窗上的tap手势，立即移除弹窗，同时销毁定时器
- (void)tapTopDownView {
    [self.topDownView hide:nil];
    [self releaseTimer];
    if (self.didTapHudBlock) {
        self.didTapHudBlock();
    }
    if (self.hideHudCompletionBlock) {
        self.hideHudCompletionBlock();
    }
}

- (void)tapHUD:(TapZSTopDownHUD)didTapHUDCallBack {
    self.didTapHudBlock = didTapHUDCallBack;
}

@end
